package com.coolpad.Utils;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.codec.binary.Base64;

public class Md5Utils {
    public Md5Utils() {
    }

    public static String getMd5Digest(String pInput) {
        try {
            MessageDigest lDigest = MessageDigest.getInstance("MD5");
            lDigest.update(pInput.getBytes());
            BigInteger lHashInt = new BigInteger(1, lDigest.digest());
            return String.format("%1$032X", lHashInt);
        } catch (NoSuchAlgorithmException var3) {
            throw new RuntimeException(var3);
        }
    }

    public static String md5(String plainText) {
        Object encryptText = null;

        try {
            MessageDigest m = MessageDigest.getInstance("MD5");
            m.update(plainText.getBytes("UTF8"));
            byte[] s = m.digest();
            String result = "";

            for(int i = 0; i < s.length; ++i) {
                result = result + Integer.toHexString(255 & s[i] | -256).substring(6);
            }

            return result;
        } catch (Exception var6) {
            return (String)encryptText;
        }
    }

    public static String md5Base64(String input) {
        try {
            MessageDigest lDigest = MessageDigest.getInstance("MD5");
            lDigest.update(input.getBytes("UTF8"));
            return Base64.encodeBase64URLSafeString(lDigest.digest());
        } catch (NoSuchAlgorithmException var2) {
            throw new RuntimeException(var2);
        } catch (UnsupportedEncodingException var3) {
            throw new RuntimeException(var3);
        }
    }

    public static String getHash(String fileName, String hashType) {
        MessageDigest md5 = null;

        try {
            byte[] buffer = new byte[1024];
            InputStream fis = new FileInputStream(fileName);
            md5 = MessageDigest.getInstance(hashType);
            boolean var5 = false;

            int numRead;
            while((numRead = fis.read(buffer)) > 0) {
                md5.update(buffer, 0, numRead);
            }

            fis.close();
        } catch (Exception var6) {
        }

        return md5 == null ? "" : toHexString(md5.digest());
    }

    private static String toHexString(byte[] b) {
        char[] hexChar = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        StringBuilder sb = new StringBuilder(b.length * 2);

        for(int i = 0; i < b.length; ++i) {
            sb.append(hexChar[(b[i] & 240) >>> 4]);
            sb.append(hexChar[b[i] & 15]);
        }

        return sb.toString();
    }

    public static String toHexStringUpperCase(byte[] b) {
        return toHexString(b).toUpperCase();
    }
}

